<script setup>
import { ref, onMounted, watch } from 'vue'
import * as echarts from 'echarts'
import { aircraftServer } from '@/api'
import { useAirStore } from '@/stores'
import { storeToRefs } from 'pinia'
import { formatTimestamp } from '@/utils/common'
const airStore = useAirStore()
const { AirFlight, FocusAirId } = storeToRefs(airStore)
// 获取飞行器信息
const aircraftInfo = ref('')
const getAircraftInfo = async () => {
  const res = await aircraftServer({ snCode: FocusAirId.value })
  aircraftInfo.value = res.data
}

const optionData = {
  animation: true,
  tooltip: {
    trigger: 'axis',
    borderColor: '#0F2638',
    backgroundColor: '#0F2638DB',
    textStyle: {
      color: '#fff',
      fontSize: 14,
      marginLeft: '-20px'
    }
  },
  grid: {
    left: '2%',
    right: '3%',
    bottom: '2%',
    top: '14%',
    containLabel: true
  },
  legend: {
    data: ['飞行高度', '飞行速度'],
    textStyle: {
      fontSize: 10,
      color: 'rgb(167,177,187)'
    },
    right: '22%'
  },
  xAxis: [
    {
      type: 'category',
      data: [],
      axisLabel: {
        textStyle: {
          color: 'rgba(255,255,255,.7)'
        }
      },
      axisLine: {
        show: true,
        lineStyle: {
          color: 'rgba(255, 255, 255, 0.2)'
        }
      },
      axisTick: {
        show: false
      }
    }
  ],
  yAxis: [
    {
      type: 'value',
      name: '高度/m',
      splitNumber: 3,
      // max: 400,
      nameTextStyle: {
        padding: [0, 10, 0, 0],
        color: 'rgba(255,255,255,.7)'
      },
      axisLabel: {
        textStyle: {
          color: 'rgba(255,255,255,.7)'
        },
        formatter: '{value}'
      },
      axisLine: {
        show: false
      },
      splitLine: {
        show: true,
        lineStyle: {
          type: 'dashed',
          color: 'rgba(255, 255, 255, 0.2)'
        }
      }, //设置网格
      axisTick: {
        show: false
      }
    },
    {
      type: 'value',
      name: '高度/m/s',
      // max: 40,
      splitNumber: 3,
      nameTextStyle: {
        padding: [0, 0, 0, 10],
        color: 'rgba(255,255,255,.7)'
      },
      axisLabel: {
        textStyle: {
          color: 'rgba(255,255,255,.7)'
        },
        formatter: '{value}'
      },
      axisLine: {
        show: false
      },
      splitLine: {
        show: false,
        lineStyle: {
          type: 'dashed',
          color: 'rgba(255, 255, 255, 0.2)'
        }
      }, //设置网格
      axisTick: {
        show: false
      }
    }
  ],
  series: [
    {
      animation: {
        // 对该系列的特定设置
        duration: 2000, // 动画持续时间，单位毫秒
        easing: 'bounceOut' // 动画缓动效果
      },
      name: '飞行高度',
      type: 'line',
      showSymbol: false,
      smooth: true,
      symbolSize: 8,
      yAxisIndex: 0,
      lineStyle: {
        color: '#3CE4E8'
      },
      itemStyle: {
        // 设置symbol的颜色
        normal: {
          color: '#3CE4E8'
        }
      },
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          {
            offset: 0,
            color: 'rgba(60, 228, 232, 0.5)'
          },
          {
            offset: 1,
            color: 'rgba(60, 228, 232, 0)'
          }
        ])
      },
      data: []
    },
    {
      name: '飞行速度',
      type: 'line',
      yAxisIndex: 1,
      smooth: true,
      showSymbol: false,
      symbolSize: 8,
      lineStyle: {
        color: '#FFF37C'
      },
      itemStyle: {
        // 设置symbol的颜色
        normal: {
          color: '#FFF37C'
        }
      },
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          {
            offset: 0,
            color: 'rgba(255, 243, 124, 0.5)'
          },
          {
            offset: 1,
            color: 'rgba(255, 243, 124, 0)'
          }
        ])
      },
      data: []
    }
  ]
}
const option = ref(optionData)

onMounted(() => {
  getAircraftInfo()
  option.value.xAxis[0].data = []
  option.value.series[0].data = []
  option.value.series[1].data = []
})

watch(
  AirFlight,
  (newVal) => {
    if (newVal[FocusAirId.value]) {
      option.value.xAxis[0].data.push(
        formatTimestamp(newVal[FocusAirId.value]?.timeStamp)?.slice(-5)
      )
      option.value.series[0].data.push(newVal[FocusAirId.value]?.height)
      option.value.series[1].data.push(newVal[FocusAirId.value]?.gs)
    }
  },
  {
    deep: true,
    immediate: true
  }
)
</script>
<template>
  <div class="card">
    <CommonTitle title="飞行器信息" />
    <div class="top">
      <div>
        <span>序列号:</span>
        <span>分类:</span>
        <span>生产厂家:</span>
        <span>飞手姓名:</span>
        <span>联系方式:</span>
      </div>
      <div>
        <span>{{ aircraftInfo.snCode }}</span>
        <span>{{ aircraftInfo.equipmentType }}</span>
        <span>{{ aircraftInfo.manufacturer }}</span>
        <span>{{ aircraftInfo.name }}</span>
        <span>{{ aircraftInfo.contacts }}</span>
      </div>
    </div>
    <div class="ma-top">
      <CommonTitle title="飞行态势信息" />
      <div class="bottom">
        <div>
          <span>飞行速度:</span>
          <span>当前经纬度:</span>
          <span>飞行时长:</span>
          <span>当前高度:</span>
          <span>更新时间:</span>
        </div>
        <div>
          <span>{{ AirFlight[FocusAirId]?.gs }}m/s</span>
          <span>{{
            AirFlight[FocusAirId]
              ? `${AirFlight[FocusAirId].longitude} , ${AirFlight[FocusAirId].latitude}`
              : ''
          }}</span>
          <span>{{ AirFlight[FocusAirId]?.flightTime }}s</span>
          <span>{{ AirFlight[FocusAirId]?.height }}m</span>
          <span>{{
            AirFlight[FocusAirId] ? formatTimestamp(AirFlight[FocusAirId].timeStamp) : ''
          }}</span>
        </div>
      </div>
    </div>
  </div>
  <div class="ma-top">
    <CommonTitle title="飞行实况" />
    <div class="chart">
      <Charts :option="option" container="7" />
    </div>
  </div>
</template>
<style scoped lang="scss">
.card {
  .top {
    height: 185px;
    padding: 18px 10px;
    display: flex;
    div:first-child {
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      align-items: center;
      width: 84px;
      color: #afebff;
      background: url('@img/operation/fly-info-left-bg.png');
      background-size: 100% 100%;
    }
    div:last-child {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      color: #ccc;
      background: url('@img/operation/fly-info-right-bg.png');
      background-size: 100% 100%;
      span {
        margin-left: 10px;
      }
    }
  }
  .bottom {
    height: 185px;
    padding: 18px 10px;
    display: flex;
    div:first-child {
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      align-items: start;
      width: 84px;
      color: #afebff;
      background: url('@img/operation/fly-info-left-bg.png');
      background-size: 100% 100%;
      span {
        padding-left: 8px;
      }
    }
    div:last-child {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      color: #ccc;
      background: url('@img/operation/fly-info-right-bg.png');
      background-size: 100% 100%;
      span {
        margin-left: 10px;
      }
    }
  }
}
.ma-top {
  margin-top: 30px;
}
.chart {
  height: 260px;
  margin-top: 30px;
  padding: 0 10px;
}
</style>
